package com.shenmazong.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.order.mapper.TbProductMapper;
import com.shenmazong.order.pojo.TbOrder;
import com.shenmazong.order.pojo.TbProduct;
import com.shenmazong.order.service.ITbUserService;
import com.shenmazong.order.service.TbOrderService;
import com.shenmazong.order.mapper.TbOrderMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.IdVo;
import com.shenmazong.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 *
 */
@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder>
    implements TbOrderService{

    @Autowired
    TbOrderMapper tbOrderMapper;

    @Autowired
    TbProductMapper tbProductMapper;

    @Autowired
    ITbUserService iTbUserService;

    @Override
    public ResultResponse add(TbOrder tbOrder) {
        //--1 检查库存
        TbProduct one = tbProductMapper.selectOne(
                new QueryWrapper<TbProduct>().lambda().eq(TbProduct::getProductId, tbOrder.getProductId()).last("limit 1")
        );
        if(one == null) {
            return ResultResponse.FAILED(404, "商品不存在");
        }

        //--2 扣库存
        if(one.getProductWare() < tbOrder.getProductCount()) {
            return ResultResponse.FAILED(406, "商品库存不足");
        }
        one.setProductWare(one.getProductWare()-tbOrder.getProductCount());
        one.setUpdateTime(null);
        tbProductMapper.updateById(one);

        //--3 生成订单
        // 1、通过前台传过来
        // 2、可以通过openfeign来获取用户信息
        //==================================
        IdVo idVo = new IdVo();
        idVo.setId(tbOrder.getUserId());
        ResultResponse resultResponse = iTbUserService.get(idVo);
        if(resultResponse.getCode() != 200) {
            return ResultResponse.FAILED(resultResponse.getCode(), resultResponse.getMsg());
        }

        HashMap<String, Object> data = (HashMap<String, Object>)resultResponse.getData();
        String userName = (String)data.get("userName");
        //===================================

        tbOrder.setUserName(userName);
        tbOrder.setProductName(one.getProductName());
        tbOrder.setProductDesc(one.getProductDesc());
        tbOrderMapper.insert(tbOrder);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse listByPage(PageVo pageVo) {
        //--1 构造page
        Page<TbOrder> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());

        //--2 构造查询条件
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        if(pageVo.getId() != null) {
            wrapper.lambda().eq(TbOrder::getUserId, pageVo.getId());
        }
        wrapper.lambda().orderByDesc(TbOrder::getOrderId);

        //--3 分页查询
        Page<TbOrder> tbOrderPage = tbOrderMapper.selectPage(page, wrapper);

        return ResultResponse.SUCCESS(tbOrderPage);
    }
}




